# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk
import jiaocaidb

class JiaocaiInputFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.JiaocaiInput_window = tk.Toplevel(self.master)
        self.JiaocaiInput_window.title('教材信息录入')
        self.JiaocaiInput_window.geometry('%dx%d' % (300, 300))
        self.page = tk.Frame(self.JiaocaiInput_window)
        self.page.pack()
        self.ISBN = tk.StringVar()
        self.jname = tk.StringVar()
        self.jauthor = tk.StringVar()
        self.price = tk.StringVar()
        self.stock = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='ISBN: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.ISBN).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='教材名: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.jname).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='作者: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.jauthor).grid(row=3, column=1, sticky=tk.E)
        tk.Label(self.page, text='单价: ').grid(row=4, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.price).grid(row=4, column=1, sticky=tk.E)
        tk.Label(self.page, text='库存: ').grid(row=5, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.stock).grid(row=5, column=1, sticky=tk.E)
        tk.Button(self.page, text='录入教材信息', command=self.record_JiaocaiInput).grid(row=6, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=7, column=1, sticky=tk.E, pady=10)

    def record_JiaocaiInput(self):
        ISBN = self.ISBN.get()
        jname = self.jname.get()
        jauthor = self.jauthor.get()
        price = self.price.get()
        stock = self.stock.get()

        if not ISBN:
            self.status.set('录入失败，ISBN不能为空')
            return

        if jiaocaidb.jiaocai_exists(ISBN):
            self.status.set('录入失败，ISBN已存在')
            return

        jiaocaidb.insert_jiaocai(ISBN, jname, jauthor, price, stock)
        self.status.set('教材信息录入成功')


class JiaocaiQueryFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.JiaocaiQuery_window = tk.Toplevel(self.master)
        self.JiaocaiQuery_window.title('教材信息查询')
        self.JiaocaiQuery_window.geometry('%dx%d' % (300, 300))
        self.page = tk.Frame(self.JiaocaiQuery_window)
        self.page.pack()
        self.ISBN = tk.StringVar()
        self.jname = tk.StringVar()
        self.jauthor = tk.StringVar()
        self.price = tk.StringVar()
        self.stock = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='ISBN: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.ISBN).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='教材名: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.jname, state='readonly').grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='作者: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.jauthor, state='readonly').grid(row=3, column=1, sticky=tk.E)
        tk.Label(self.page, text='单价: ').grid(row=4, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.price, state='readonly').grid(row=4, column=1, sticky=tk.E)
        tk.Label(self.page, text='库存: ').grid(row=5, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.stock, state='readonly').grid(row=5, column=1, sticky=tk.E)
        tk.Button(self.page, text='查询教材信息', command=self.query_JiaocaiInfo).grid(row=6, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=7, column=1, stick=tk.E, pady=10)

    def query_JiaocaiInfo(self):
        ISBN = self.ISBN.get()
        result = jiaocaidb.query_by_ISBN(ISBN)
        if result:
            self.jname.set(result['jname'])
            self.jauthor.set(result['jauthor'])
            self.price.set(result['price'])
            self.stock.set(result['stock'])
            self.status.set('查询成功')
        else:
            self.jname.set('')  # 如果没有找到信息，清空输入框
            self.jauthor.set('')
            self.price.set('')
            self.stock.set('')
            self.status.set('没有找到对应的教材信息')


class JiaocaiChangeFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.JiaocaiChange_window = tk.Toplevel(self.master)
        self.JiaocaiChange_window.title('教材信息修改')
        self.JiaocaiChange_window.geometry('%dx%d' % (300, 300))
        self.page = tk.Frame(self.JiaocaiChange_window)
        self.page.pack()
        self.ISBN = tk.StringVar()
        self.jname = tk.StringVar()
        self.jauthor = tk.StringVar()
        self.price = tk.StringVar()
        self.stock = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='ISBN: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.ISBN).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='教材名: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.jname).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='作者: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.jauthor).grid(row=3, column=1, sticky=tk.E)
        tk.Label(self.page, text='单价: ').grid(row=4, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.price).grid(row=4, column=1, sticky=tk.E)
        tk.Label(self.page, text='库存: ').grid(row=5, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.stock).grid(row=5, column=1, sticky=tk.E)
        tk.Button(self.page, text='修改教材信息', command=self.change_JiaocaiInfo).grid(row=6, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=7, column=1, stick=tk.E, pady=10)

    def change_JiaocaiInfo(self):
        ISBN = self.ISBN.get()
        data = {
            'jname': self.jname.get(),
            'jauthor': self.jauthor.get(),
            'price': self.price.get(),
            'stock': self.stock.get(),
        }
        result = jiaocaidb.query_by_ISBN(ISBN)
        if result:
            jiaocaidb.update_by_ISBN(ISBN, data)
            self.status.set('修改成功')
        else:
            self.status.set('没有找到对应的教材信息')
